// This class has the only goal to increment a counter which 
// is in fact a sharedObjects until a certain value


public class StressWrite extends Thread {
	
	public static void main(String argv[]) throws InterruptedException{
		
		if (argv.length != 1) {
			System.out.println("java StressWrite <nb iteration>");
			return;
		}
		int nb = Integer.parseInt(argv[0]);
		
		// initialize the system
		Client.init();
		
		// look up the counter object in the name server
		// if not found, create it, and register it in the name server
		
		SharedObject c = Client.lookup("Counter");
		
		if (c == null) {
			c = Client.create(new Counter());
			Client.register("Counter", c);
		}	
		
		sleep(1000);
		System.out.println("Writer started");
		
		//int j;
		for (int i=1; i<=nb; i++){
			
			/*c.lock_read();
			j = Integer.parseInt(c.read());
			c.unlock();*/

			c.lock_write();
			if(i%(nb/50) == 1)
				System.out.println("write " + i);
			((Counter) c.obj).write(i);
			c.unlock();
			
		}
		sleep(2000);
		System.out.println("Writer terminated");
	}
}